package Servlet;

import DAO.DAOFactory;
import DAO.PackageDAO;
import javabean.Customer;
import javabean.CustomerPackage;
import javabean.Package;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

import static sun.text.normalizer.Utility.escape;

@WebServlet("/MyPackage")
public class MyPackageServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        HttpSession session = req.getSession();
        PackageDAO packageDAO = (PackageDAO) DAOFactory.getPackageDAOInstance();
        Customer customer = (Customer) session.getAttribute("loginCustomer"); // login user
        try {
            if (packageDAO.GetCustomerPackage(customer)!=null) {
                CustomerPackage  customerPackage = packageDAO.GetCustomerPackage(customer);
                Package pack = new Package();
                pack.setPackageID(customerPackage.getPackageID());
                session.setAttribute("curPackage", customerPackage);
                req.getRequestDispatcher("MyPackage.jsp").forward(req, resp);
            } else {
//                req.getRequestDispatcher("Package_Subscription").forward(req, resp);
                PrintWriter out = resp.getWriter();
                out.println("<script>");
                out.println("if(confirm('" + escape("您还未订购任何套餐，是否前往套餐订购页面？") + "')) {");
                out.println("window.location.href='Subpackage';");
                out.println("} else {");
                out.println("  " + "window.location.href='Personal_Info.jsp';"); // 在这里添加重定向到个人信息页面的代码
                out.println("}");
                out.println("</script>");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

